<template>
  <div>
    <el-form :model="formData">
      <el-form-item label="字典名称" label-width="100px">
        <el-input v-model="formData.dicname"></el-input>
      </el-form-item>
      <el-form-item label="字典编码" label-width="100px">
        <el-input v-model="formData.code" :disabled="id || childId ? true : false"></el-input>
      </el-form-item>
      <el-form-item label="描述" label-width="100px">
        <el-input v-model="formData.desc"></el-input>
      </el-form-item>
      <el-form-item label="排序" label-width="100px" v-show="id || childId">
        <el-input v-model="formData.sort"></el-input>
      </el-form-item>
      <el-form-item label="字典状态" label-width="100px" v-show="id || childId">
        <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%;">
          <el-option label="启用" :value="1"></el-option>
          <el-option label="未启用" :value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'AddDic',
  props: ['id','parentid','childId'],
  data() {
    return {
      formData: {
        id: '',
        dicname: '',
        code: '',
        desc: '',
        sort: '0',
        status: '1',
        parentid: '0',
      }
    }
  },
  mounted() {
    this.$bus.$on('toAddDic', this.toAddDic);
    // 儿子接口
    if(this.parentid){
      this.$bus.$on('toAddChildDic',this.toAddChildDic);
      if(this.childId){
        this.getChildDicById();
      }
    }
    if(this.id){
      this.getDicById();
    }
  },
  beforeDestroy() {
    this.$bus.$off('toAddDic');
    // 儿子接口
    if(this.parentid){
      this.$bus.$off('toAddChildDic');
    }
  },
  methods: {
    async getDicById() {
      let res = await this.$API.SystemMenus.reqSelectById(this.id);
      if (res.code == 200) {
        this.formData = { ...res.data }
      }
    },
    async toAddDic() {
      if (this.id) {
        let {dicname, code, desc, sort, status} = this.formData;
        let res = await this.$API.SystemMenus.reqUpdateDirct1(this.id,dicname,code,desc,sort,status);
        if(res.code == 200){
          this.$message({ message: '修改成功', type: 'success' });
        }
      } else {
        let { id, dicname, code, desc, sort, status, parentid } = this.formData;
        let res = await this.$API.SystemMenus.reqAddDirct1(id = 0, dicname, parentid, code, desc, sort, status);
        if (res.code == 200) {
          this.$message({ message: '添加成功', type: 'success' });
        }
      }
    },


    // 子集接口
    async getChildDicById(){
      let res = await this.$API.SystemMenus.reqChildDicById(this.childId);
      if (res.code == 200) {
        this.formData = { ...res.data }
      }
    },
    // 儿子字典
    async toAddChildDic(){
      // /addDict2?id=0&dicname=123&parentid=46&desc=1&code=1&sort=0&status=1
      if (this.childId) {
        let {dicname, code, desc, sort, status} = this.formData;
        let res = await this.$API.SystemMenus.reqUpdateChildDic(this.childId,dicname,desc,code,sort,status);
        if(res.code == 200){
          this.$message({ message: '修改成功', type: 'success' });
        }
      } else {
        let { dicname, code, desc, sort } = this.formData;
        let res = await this.$API.SystemMenus.reqAddChildDic(dicname,this.parentid,desc,code,sort);
        if (res.code == 200) {
          this.$message({ message: '添加成功', type: 'success' });
        }
      }
    }
  }
};
</script>

<style scoped></style>
